import Vue from 'vue'
import VueRouter from 'vue-router'
import Movie from '../views/home/Movie.vue'

//使用路由插件
Vue.use(VueRouter)

//定义路由表
const routes = [{
    path: "/",
    redirect: "/home"
  },
  {
    path: '/home',
    name: 'Movie',
    component: Movie,
    children: [{
        path: '/home',
        redirect: '/home/nav/hot'
      },
      {
        path: "nav/hot",
        name: 'Hot',
        component: () => import( /* webpackChunkName: "hot" */ '../views/home/navs/Hot.vue')
      },
      {
        path: "nav/cinema",
        name: 'Cinema',
        component: () => import( /* webpackChunkName: "cinema" */ '../views/home/navs/Cinema.vue')
      },
      {
        path: "nav/daiying",
        name: 'DaiYing',
        component: () => import( /* webpackChunkName: "daiying" */ '../views/home/navs/Daiying.vue')
      },
      {
        path: "nav/classicmovie",
        name: 'ClassicMovie',
        component: () => import( /* webpackChunkName: "classicmovie" */ '../views/home/navs/ClassicMovie.vue')
      }
    ]
  },
  {
    path: '/video',
    name: 'Video',
    //路由懒加载，作用：优化性能，点击后显示这个页面时才加载页面内容
    component: () => import( /* webpackChunkName: "video" */ '../views/Video.vue')
  },
  {
    path: '/minivideo',
    name: 'MiniVideo',
    component: () => import( /* webpackChunkName: "minivideo" */ '../views/MiniVideo.vue')
  },
  {
    path: '/show',
    name: 'Show',
    component: () => import( /* webpackChunkName: "show" */ '../views/Show.vue')
  },
  {
    path: '/me',
    name: 'Me',
    component: () => import( /* webpackChunkName: "me" */ '../views/Me.vue')
  },
  {
    //内容页的路由
    path: '/item', //动态路由
    name: 'Item',
    component: () => import( /* webpackChunkName: "item" */ '../views/Item.vue')
  },
  {
    //登录页的路由
    path: '/login', //动态路由
    name: 'login',
    component: () => import( /* webpackChunkName: "login" */ '../views/Login.vue')
  },
  {
    //城市页的路由
    path: '/city', //动态路由
    name: 'City',
    component: () => import( /* webpackChunkName: "city" */ '../views/City.vue')
  },
  { //404页面路由配置，必须放在路由表的最后一句
    path: '*',
    name: 'page404',
    component: () => import( /* webpackChunkName: "page404" */ '../views/Page404.vue')
  }
]

//创建路由对象
const router = new VueRouter({
  routes
})

//添加全局守卫
router.beforeEach((to, from, next) => {
  switch (to.path) {
    case '/me': //用户中心，需要守卫
      //需要判断登录状态 ,我把登录状态放到了 sessionStorage中
      if(sessionStorage.getItem('loginType')){
        next();
      }else{
        next('/login');
      }
      break;
    default:
      next();
      break;
  }
})

//输出
export default router